package com.ayoka.parenting.ui.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.ayoka.parenting.R;
import com.ayoka.parenting.mvp.MVPBaseActivity;
import com.jincp.common.utils.MyLogger;

import android.databinding.ViewDataBinding;
import android.support.v4.app.FragmentManager;

/**
 * @ProjectName: MVPPlugin.
 * @ClassName: MainActivity.
 * @Description: .
 * @Author: jincp.
 * @CreateDate: 2019/10/10 18:17.
 */
public class MainActivity extends MVPBaseActivity<ViewDataBinding, MainContract.View, MainPresenter> implements MainContract.View {


    public static void start(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        activity.startActivity(intent);
    }


    @Override
    protected int getContentLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        if (mPresenter != null) {
            mPresenter.getUserInformation();
        }
        configFragments();
    }

    @Override
    protected MainPresenter getPresenter() {
        return new MainPresenter();
    }

    public void configFragments() {
        FragmentManager manager = getSupportFragmentManager();
        NavigationFragment navigationFragment = ((NavigationFragment) manager.findFragmentById(R.id.fragment_navigation));
        navigationFragment.setup(manager, R.id.frameLayout);
    }

    @Override
    public void onBackPressed() {
        exit();
    }

    //声明一个long类型变量：用于存放上一点击“返回键”的时刻
    private long mExitTime;
    private void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            //大于2000ms则认为是误操作，使用Toast进行提示
            showToast("再按一次退出程序");
            //并记录下本次点击“返回键”的时刻，以便下次进行判断
            mExitTime = System.currentTimeMillis();
        } else {
            //小于2000ms则认为是用户确实希望退出程序-调用System.exit()方法进行退出
            finish();
        }
    }

    @Override
    public void onGetUserInformationCallback(boolean isSuccess) {

    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.quitTIC();
        }
        super.onDestroy();
    }
}
